/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ec.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ec.entity.ItemType;
import com.thinkgem.jeesite.modules.ec.service.ItemTypeService;

/**
 * 商品类型Controller
 * @author zkool
 * @version 2016-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/ec/itemType")
public class ItemTypeController extends BaseController {

	@Autowired
	private ItemTypeService itemTypeService;
	
	@ModelAttribute
	public ItemType get(@RequestParam(required=false) String id) {
		ItemType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = itemTypeService.get(id);
		}
		if (entity == null){
			entity = new ItemType();
		}
		return entity;
	}
	
	@RequiresPermissions("ec:itemType:view")
	@RequestMapping(value = {"list", ""})
	public String list(ItemType itemType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ItemType> page = itemTypeService.findPage(new Page<ItemType>(request, response), itemType); 
		model.addAttribute("page", page);
		return "modules/ec/itemTypeList";
	}

	@RequiresPermissions("ec:itemType:view")
	@RequestMapping(value = "form")
	public String form(ItemType itemType, Model model) {
		model.addAttribute("itemType", itemType);
		return "modules/ec/itemTypeForm";
	}

	@RequiresPermissions("ec:itemType:edit")
	@RequestMapping(value = "save")
	public String save(ItemType itemType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, itemType)){
			return form(itemType, model);
		}
		itemTypeService.save(itemType);
		addMessage(redirectAttributes, "保存商品类型成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemType/?repage";
	}
	
	@RequiresPermissions("ec:itemType:edit")
	@RequestMapping(value = "delete")
	public String delete(ItemType itemType, RedirectAttributes redirectAttributes) {
		itemTypeService.delete(itemType);
		addMessage(redirectAttributes, "删除商品类型成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemType/?repage";
	}

}